//
//  HomeSectionView.m
//  Nidalee
//
//  Created by 李康 on 2020/12/3.
//  Copyright © 2020 李康. All rights reserved.
//

#import "HomeSectionView.h"
#import <Masonry/Masonry.h>
#import "BaseMacroConfig.h"

@interface HomeSectionView()

@property (nonatomic, strong)UIButton *moreBtn;

@property (strong, nonatomic) UILabel *titleLab;

@end

@implementation HomeSectionView

#pragma mark - Life Cycle
- (instancetype)init{
    return [self initWithFrame:CGRectZero];
}
- (instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        [self setup];
    }
    return self;
}


#pragma mark - Private Methods
- (void)setup{
    self.titleLab = [[UILabel alloc]init];
    self.titleLab.font = GetRegularFont(17);
    self.titleLab.textColor = HexColor(0x333333);
    [self addSubview:self.titleLab];
    
    self.moreBtn = [[UIButton alloc]init];
    
    [self.moreBtn setTitleColor:HexColor(0x5086FC) forState:UIControlStateNormal];
    self.moreBtn.titleLabel.font = GetRegularFont(14);
    [self.moreBtn setTitle:@"查看更多" forState:UIControlStateNormal];
    [self.moreBtn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:self.moreBtn];
    [self.titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
       make.left.equalTo(self).offset(15*M6Scale);
       make.centerY.equalTo(self);
    }];
    [self.moreBtn mas_makeConstraints:^(MASConstraintMaker *make) {
       make.right.equalTo(self).offset(-15*M6Scale);
       make.centerY.equalTo(self);
    }];
}

#pragma mark - Getter & Setter
-(void)setTitleString:(NSString *)titleString{
    _titleString =  titleString;
    self.titleLab.text = titleString;
}

/** 获取最顶层ViewController */
- (UIViewController *)topViewController {
    UIViewController *resultVC;
    resultVC = [self _topViewController:[[UIApplication sharedApplication].keyWindow rootViewController]];
    while (resultVC.presentedViewController) {
        resultVC = [self _topViewController:resultVC.presentedViewController];
    }
    return resultVC;
}

- (UIViewController *)_topViewController:(UIViewController *)vc {
    if ([vc isKindOfClass:[UINavigationController class]]) {
        return [self _topViewController:[(UINavigationController *)vc topViewController]];
    } else if ([vc isKindOfClass:[UITabBarController class]]) {
        return [self _topViewController:[(UITabBarController *)vc selectedViewController]];
    } else {
        return vc;
    }
    return nil;
}

-(void)btnClick:(id)sender{
    if (self.callBack) {
        self.callBack(@"1");
    }
}

@end
